package bigdata;

import java.util.HashMap;

import backtype.storm.utils.Utils;

/*
 * the UpdateAvergeThread class calculates the average number of visited pages each minute
 */
public class UpdateAvergeThread implements Runnable{

	public static int pageVisitPerMinAvg = 4;
	public static int pagesVisitedInLastMin = 0;
	
	public static int searchesPerMinAvg = 4;
	public static int searchesInLastMin = 0;
	
	public static HashMap<String,Integer> userPageVisitInLastMinMap =new HashMap<String,Integer>(); 
	public static HashMap<String, Integer> userSearchInLastMinMap = new HashMap<String, Integer>();
	   
	final long MINUTE = 1000*10; 
	
	private boolean debug = true;
	
	@Override
	public void run() {
		SimulationMain.gui.lblPageViews.setText(String.valueOf(pageVisitPerMinAvg));
		SimulationMain.gui.lblSearch.setText(String.valueOf(searchesPerMinAvg));
		if (!debug) {
			while (true) {
			
				pagesVisitedInLastMin=0;
				searchesInLastMin=0;
				
				userPageVisitInLastMinMap.clear();
				userSearchInLastMinMap.clear();
				
				UpdateBolt.timeUserOnSamePage.clear();
				UpdateBolt.currUserSite.clear();
				
				 Utils.sleep(MINUTE);
				 
				 if (userPageVisitInLastMinMap.size()>0) {
					 pageVisitPerMinAvg = pagesVisitedInLastMin/ userPageVisitInLastMinMap.size();
					 SimulationMain.gui.lblPageViews.setText(String.valueOf(pageVisitPerMinAvg));
				 }
					 
				 if (userSearchInLastMinMap.size()>0) {
					 searchesPerMinAvg = searchesInLastMin / userSearchInLastMinMap.size();
					 SimulationMain.gui.lblSearch.setText(String.valueOf(searchesPerMinAvg));
				 }
	
				 SimulationMain.gui.addStringToLog("new pageVisitPerMinAvg:"+pageVisitPerMinAvg);
				 SimulationMain.gui.addStringToLog("new searchesPerMinAvg:"+searchesPerMinAvg);
	
			}
		}
	}
	
	
	

}
